package com.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.commonutils.RandomUtil;
import com.entity.pojo.Categories;
import com.entity.pojo.Concept;
import com.entity.pojo.Courses;
import com.entity.pojo.Resources;
import com.service.CategoriesService;
import com.service.ConceptService;
import com.service.CoursesService;
import com.service.ResourcesService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class InitData {
    @Resource
    private CoursesService coursesService;

    @Resource
    private ResourcesService resourcesService;

    @Resource
    private CategoriesService categoriesService;

    @Resource
    private ConceptService conceptService;

    // 初始化资源信息
    public void initResource(Resources newResource){
        // 设置排序字段值
        // 查找同级知识点下的所有资源
        List<Resources> list = resourcesService.list(new QueryWrapper<Resources>()
                .eq("conceptId", newResource.getConceptId())
                .select("positionId"));
        int maxPositionId;
        if (list.isEmpty()) {
            maxPositionId = 0;
        } else {
            maxPositionId = list.stream().mapToInt(Resources::getPositionId).summaryStatistics().getMax();
        }
        newResource.setPositionId(maxPositionId + 1);
        // 设置目录代码
        QueryWrapper<Resources> wrapper = new QueryWrapper<Resources>().select("max(resourceId) as resourceId");
        Resources resource = resourcesService.getOne(wrapper);
        int maxPrimaryId = resource == null ? 1 : resource.getResourceId();
        StringBuilder maxPrimaryIdStr = new StringBuilder(Integer.toString(maxPrimaryId));
        while (maxPrimaryIdStr.length() < 5) {
            maxPrimaryIdStr.insert(0, "0");
        }
        String code = "D" + RandomUtil.getSixBitRandom() + maxPrimaryIdStr;
        newResource.setResourceCode(code);
    }

    // 初始化知识点信息
    public void initConcept(Concept concept){
        // 设置排序字段值
        // 查找同级目录下的所有知识点
        List<Concept> list = conceptService.list(new QueryWrapper<Concept>()
                .eq("categoryId", concept.getCategoryId())
                .select("positionId"));
        int maxPositionId;
        if (list.isEmpty()) {
            maxPositionId = 0;
        } else {
            maxPositionId = list.stream().mapToInt(Concept::getPositionId).summaryStatistics().getMax();
        }
        concept.setPositionId(maxPositionId + 1);
        // 设置目录代码
        QueryWrapper<Concept> wrapper = new QueryWrapper<Concept>().select("max(conceptId) as conceptId");
        Concept concepts = conceptService.getOne(wrapper);
        int maxPrimaryId = concepts == null ? 1 : concepts.getConceptId();
        StringBuilder maxPrimaryIdStr = new StringBuilder(Integer.toString(maxPrimaryId));
        while (maxPrimaryIdStr.length() < 5) {
            maxPrimaryIdStr.insert(0, "0");
        }
        String code = "C" + RandomUtil.getSixBitRandom() + maxPrimaryIdStr;
        concept.setConceptCode(code);
    }

    // 初始化目录信息
    public void initCategory(Categories category){
        // 设置排序字段值
        List<Categories> list = categoriesService.list(new QueryWrapper<Categories>()
                .eq("courseId", category.getCourseId())
                .select("positionId"));
        int maxPositionId;
        if (list.isEmpty()) {
            maxPositionId = 0;
        } else {
            maxPositionId = list.stream().mapToInt(Categories::getPositionId).summaryStatistics().getMax();
        }
        category.setPositionId(maxPositionId + 1);
        // 设置目录代码
        QueryWrapper<Categories> wrapper = new QueryWrapper<Categories>().select("max(categoryId) as categoryId");
        Categories categories = categoriesService.getOne(wrapper);
        int maxPrimaryId = categories == null ? 1 : categories.getCategoryId();
        StringBuilder maxPrimaryIdStr = new StringBuilder(Integer.toString(maxPrimaryId));
        while (maxPrimaryIdStr.length() < 5) {
            maxPrimaryIdStr.insert(0, "0");
        }
        String code = "B" + RandomUtil.getSixBitRandom() + maxPrimaryIdStr;
        category.setCategoryCode(code);
    }

    // 初始化课程信息
    public void initCourse(Courses course) {
        QueryWrapper<Courses> wrapper = new QueryWrapper<Courses>().select("max(courseId) as courseId");
        // 设置课程代码
        Courses courses = coursesService.getOne(wrapper);
        int maxPrimaryId = courses == null ? 1 : courses.getCourseId();
        StringBuilder maxPrimaryIdStr = new StringBuilder(Integer.toString(maxPrimaryId));
        while (maxPrimaryIdStr.length() < 5) {
            maxPrimaryIdStr.insert(0, "0");
        }
        String code = "A" + RandomUtil.getSixBitRandom() + maxPrimaryIdStr;
        course.setCourseCode(code);
    }
}
